<?php
ArrowController::importModel("org.arrowplatform.controls.presentation.table.columns.ArrowAbstractTableColumn");
ArrowController::importModel("smarty.SmartyCompiler");
class ArrowTableMenuColumn extends ArrowAbstractTableColumn{
	
	private $compiledLink;
	private $transport;
	private $text;
	private $menuName;
	
	public function configure(){
		parent::configure();
		$this->addProperties(array(
			"transport" => false, 
			"menuName" => null, 
			"template" => false,
			"pwidth" => 100,
			"pheight" => 100
		));
		$this->addRequiredProperties(array( "menuName" ));
		$this->setContentCodeKept(true);
		
	}

	public function init(){
		parent::init();
		
		$tmpArgs = array();
		$tmpArgs["model"] = "#model#";
		$tmpArgs["key"] = "#key#";
		
		$this->transport = $this->getProperty("transport");
		$this->menuName = $this->getProperty("menuName");
		
		 
		$this->compiledLink = $this->getStateChangeLink(  $tmpArgs , true, false  );
		
		if($this->getProperty("template")){
			$tmpArgs["path"] = $this->getProperty("template");
			$tmpArgs["transport"] = "#transport#";
			$this->compiledLink = ArrowTemplateLinker::getDefault()->generateTemplateLink($tmpArgs);
		}
		
		$data = array(
			"width" => $this->getProperty("pwidth"),
			"height" => $this->getProperty("pheight"),
		);
		
		if(empty($this->class)) $this->class = 'class=" "';
		
		$currClass = self::CTRL_NAMESPACE.'-table-cell-menu-'.str_replace( ":", "-",$this->getProperty("name"));
		$this->class = trim($this->class, "\"")." ctrl-table-cell-menu ".self::CTRL_NAMESPACE.'-table-cell-menu  '.$currClass." \""; 
		
		
		$this->setProperty("name", "");
		$this->registerJs( get_class($this), dirname(__FILE__)."/resources/".get_class($this).".js", ".".$currClass , $data );
		
	}
	
	public function generateRowCell( $context ){
		$args = array();
		$args["#model#"] = $context->getModel();
		$args["#key#"] = $context->getPKey();
		$args["#transport#"] = $this->transport;
		
		$href = strtr($this->compiledLink, $args);
	
		$text = ($this->text)?$this->text:$context[$this->field];	
		$str = '<td '.$this->class.' url="'.$href.'" style="background-image: url('.$this->getImage($context, true).');" >';
		$str.= $this->menuName;
		$str.='</td>';
		return $str;
	}
	
	public function generateBlockStart(){
		$class = ArrowModel::getModelClass( $this->getState("model") );
		$context = call_user_func(array( $class, "getByKey" ),$this->getState("key"), $class );
		return $this->getFirstChild("ArrowCtrlTemplate")->processTemplate($context);
	}
	
	public function generateBlockEnd(){
		return "";
	}
	

}

?>